<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;

use app\models\ProjectFhHistory;
use app\models\User;
use app\modules\v1\controllers\parent\ParentController;
use yii\data\ActiveDataProvider;
use Yii;

class ProjectFhHistoryController extends ParentController
{
    // 获取我的任务
    public function actionIndex() {
        $params = Yii::$app->request->getQueryParams();
        $query = ProjectFhHistory::find()->where(['project_id' => $params['project_id']]);

        if (isset($params['status'])) {
            $query->andWhere(['status'=>  $params['status']]);
        }
        if (isset($params['start_at'])) {
            $query->andWhere(['>=', 'hj_project.plan_started_at', $params['start_at'] / 1000]);
        }

        if (isset($params['end_at'])) {
            $query->andWhere(['<=', 'hj_project.plan_started_at', $params['end_at'] / 1000]);
        }
        $pageSize = 5;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);
    }


    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate() {
        $data = Yii::$app->request->getBodyParams();
        $model = new ProjectFhHistory();
        $model->load($data, '');
        $model->inputer = \Yii::$app->user->id;
        $model->update_at = time();

        if ($model->save()) {
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $model
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $model->errors
        ];
    }
}




